package com.simple.datax.bo.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.simple.datax.bo.IDataxFlowTemplateBO;
import com.simple.datax.dao.IDataxFlowActivityTemplateDAO;
import com.simple.datax.dao.IDataxFlowActorsTemplateDAO;
import com.simple.datax.dao.IDataxFlowTemplateDAO;
import com.simple.datax.vo.DataxFlowActivityTemplateVO;
import com.simple.datax.vo.DataxFlowActorsTemplateVO;
import com.simple.datax.vo.DataxFlowTemplateVO;
import com.simple.util.common.Constants;
import com.simple.util.common.DateUtil;
import com.simple.util.exception.SimpleApplicationException;
import com.simple.util.object.PageInfoBean;

@Service("dataxFlowTemplateBO")
@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
public class DataxFlowTemplateBOImpl implements IDataxFlowTemplateBO {

	@Autowired
	private IDataxFlowTemplateDAO dataxFlowTemplateDAO;
	
	@Autowired
	private IDataxFlowActivityTemplateDAO dataxFlowActivityTemplateDAO;
	
	@Autowired
	private IDataxFlowActorsTemplateDAO dataxFlowActorsTemplateDAO;
	
	@Override
	public List<DataxFlowTemplateVO> findAll() {
		return dataxFlowTemplateDAO.findAll();
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
	public void add(DataxFlowTemplateVO dataxFlowTemplateVO) {
		dataxFlowTemplateDAO.saveOrUpdate(dataxFlowTemplateVO);
	}
	
	@Override
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
	public void del(String id) {
		DataxFlowTemplateVO dataxFlowTemplateVO = dataxFlowTemplateDAO.findById(id);
		dataxFlowTemplateVO.setStatus(new BigDecimal(Constants.TEMPALTE_STATE_DEL));
		dataxFlowTemplateDAO.update(dataxFlowTemplateVO);
	}

	@Override
	public DataxFlowTemplateVO findById(String id) {
		return dataxFlowTemplateDAO.findById(id);
	}

	@Override
	public List<DataxFlowTemplateVO> findListByPage(String templateName,
			String templateId, String businessType, String creatTimeStart,
			String creatTimeEnd, String creater,String status, PageInfoBean pageInfoBean) {
		return dataxFlowTemplateDAO.findListByPage(templateName, templateId, businessType, creatTimeStart, creatTimeEnd, creater , status, pageInfoBean);
	}
	@Override
	public List<DataxFlowTemplateVO> findAllActive() {
		
		return dataxFlowTemplateDAO.findAllActive();
	}

	@Override
	public String generateId() {
		return dataxFlowTemplateDAO.generateId();
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
	public void submit(String isEdit,DataxFlowTemplateVO dataxFlowTemplateVO,
			List<DataxFlowActivityTemplateVO> dataxFlowActivityTemplateVOList,
			List<DataxFlowActorsTemplateVO> dataxFlowActorsTemplateVOList) throws SimpleApplicationException {
		String flowTemplateId=dataxFlowTemplateVO.getFlowTemplateId();
		
		if(Constants.STATE_YES.equals(isEdit)){
			final DataxFlowTemplateVO oldDataxFlowTemplateVO=dataxFlowTemplateDAO.findById(flowTemplateId);
			if(oldDataxFlowTemplateVO==null){
				throw new SimpleApplicationException("message.businessflow.dataxflowtemplatevo.not.exist");
			}
			if(!Constants.TEMPALTE_STATE_TEMP.equals(oldDataxFlowTemplateVO.getStatus().toString())){
				throw new SimpleApplicationException("message.businessflow.Edit");
			}
			oldDataxFlowTemplateVO.setFlowTemplateName(dataxFlowTemplateVO.getFlowTemplateName());
			oldDataxFlowTemplateVO.setBusinessType(dataxFlowTemplateVO.getBusinessType());
			oldDataxFlowTemplateVO.setCreatCorp(dataxFlowTemplateVO.getCreatCorp());
			oldDataxFlowTemplateVO.setNotes(dataxFlowTemplateVO.getNotes());
			
			dataxFlowActivityTemplateDAO.deleteByFlowTemplateId(flowTemplateId);
			dataxFlowActorsTemplateDAO.deleteByFlowTemplateId(flowTemplateId);
			dataxFlowTemplateDAO.saveOrUpdate(oldDataxFlowTemplateVO);
		}else{
			dataxFlowTemplateVO.setStatus(new BigDecimal( Constants.TEMPALTE_STATE_SUBMIT));
			dataxFlowTemplateVO.setCreatTime(DateUtil.getNowDate());
			dataxFlowTemplateDAO.saveOrUpdate(dataxFlowTemplateVO);
		}
		
		dataxFlowActivityTemplateDAO.addBatch(dataxFlowActivityTemplateVOList);
		dataxFlowActorsTemplateDAO.addBatch(dataxFlowActorsTemplateVOList);			
	}

}